﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace mapeditor
{
    class map
    {
        private string path;
        static private int taille = (800 / 34) * (600 / 34) + 150;
        public table2d caisseList = new table2d(taille);
        private int k = 0;
        private Sprite caisse = new Sprite();
        
        public map(string path)
        {
            this.path = path;
        }

        public void Load(ContentManager content)
        {
            caisseList.Init();
            caisse.Load(content, @"images/caisse");

            using (StreamReader sr = File.OpenText("map.txt"))
            {
                string line;
                int start, end, type, x, y;

                for (int i = 0; (line = sr.ReadLine()) != null; i++)
                {
                    start = 0;
                    for (end = start; line[end] != ' '; end++) ;
                    type = int.Parse(line.Substring(start, (end - start)));

                    start = end + 1;
                    for (end = start; line[end] != ' '; end++) ;
                    x = int.Parse(line.Substring(start, (end - start)));

                    start = end + 1;
                    for (end = start; line[end] != ' ' && end < line.Length - 1; end++) ;
                    y = int.Parse(line.Substring(start, (end - start + 1)));

                    caisseList.tableau[i, 0] = type;
                    caisseList.tableau[i, 1] = x;
                    caisseList.tableau[i, 2] = y;
                    k = i;
                }
                k++;

                sr.Close();
            }
        }

        public void Draw(SpriteBatch spriteBatch, GraphicsDeviceManager graphics)
        {
            Vector2 coord = new Vector2(0, 0);
            
            for (int i = 0; i < caisseList.nbre_cases; i++)
            {
                if (caisseList.tableau[i, 1] >= 0 && caisseList.tableau[i, 1] <= graphics.PreferredBackBufferWidth && caisseList.tableau[i, 2] <= graphics.PreferredBackBufferHeight && caisseList.tableau[i, 2] >= 0)
                {
                    if (caisseList.tableau[i, 0] == 1)
                    {
                        coord.X = caisseList.tableau[i, 1];
                        coord.Y = caisseList.tableau[i, 2];

                        spriteBatch.Draw(caisse.Texture, coord, Color.White);
                    }
                }
            }
        }
    }
}
